<template>
	<view class="container">
		<uni-section title="表头信息" type="line">
			<view class="example">

				<!-- 展示不同的排列方式 -->
				<uni-forms ref="baseForm" :modelValue="baseData" :rules="rules">
					<uni-forms-item label="编号" required name="EnCode">
						<uni-easyinput v-model="baseData.EnCode" placeholder="请输入编号" />
					</uni-forms-item>
					<uni-forms-item label="名称" required name="Name">
						<uni-easyinput v-model="baseData.Name" placeholder="请输入名称" />
					</uni-forms-item>
					
					<uni-forms-item label="仓库" required name="WarehouseId">
						<uni-data-select v-model="baseData.WarehouseId" :localdata="Warehouse"></uni-data-select>
					</uni-forms-item>
					
					<uni-forms-item label="状态" required name="EnabledMark">
						<switch :checked="EnabledMark" @change="changeIndicatorDots" />
					</uni-forms-item>
					
				</uni-forms>
				<button type="primary" @click="submit('baseForm')">保存</button>
			</view>
		</uni-section>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				Warehouse: [],
				EnabledMark: true,
				baseData: {
					
					WarehouseId: '',
					Name: '',
					EnCode: '',
					EnabledMark: '',
				},
				rules: {
					// 对name字段进行必填验证
					EnCode: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写编号',
							},

						],

					},
					Name: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写名称',
							},

						],

					},
				
					WarehouseId: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写仓库',
							},

						],

					},
					
				},
				
			
		}
		},
		onLoad() {
			this.$api.Request({
				url: "/api/BasicData/GetWarehouse",
				method: 'GET',
				data: {},
			}).then((res) => {

				res.forEach(item => {

					this.Warehouse.push({
						value: item.Id,
						text: item.WarehouseName
					})

				});

			})
			

		},
		methods: {
			submit(ref) {
				this.baseData.EnabledMark=this.EnabledMark;
				this.$refs[ref].validate().then(res => {
					this.$api.Request({
						url: "/api/TakeStock/SubmitFormHead",
						method: 'POST',
						data: this.baseData,
					}).then((res) => {
						if (res.state == 'success') {
							uni.showToast({
								title: `提交成功`,
								
							})
							setTimeout(()=>{
								uni.navigateTo({
								url: '../takestockheadlist/takestockheadlist'
							  })	
							},1000)
							
						} else {

							uni.showToast({
								title: `提交失败`,
								icon:'error',
							})
						}
					})

				}).catch(err => {
					console.log('err', err);
				})
			},
			
		onNavigationBarButtonTap() {  
		    this.submit('baseForm');
		    } ,
			changeIndicatorDots(e) {
			            this.EnabledMark = !this.EnabledMark
			        },
		}
	}
</script>

<style>

</style>

